/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bugsandcritters;
import java.awt.*;
/**
 *
 * @author Robert Gibbons
 */
public class GameObject extends Thread{
    
    double X,Y,TargetX,TargetY,FromX,FromY,InterPol;
    double MoveSpeed = 0.025;
    
    private long LastNanoTime;
    long DeltaTime;
    private long MinUpdateTime = 1000000000/60;
    
    public Boolean ShouldDestroy = false;
    public Boolean NeedsUpdate = true;
    
    @Override
    public void run()
    {
        LastNanoTime = System.nanoTime();

        while(!ShouldDestroy)
        {
            if(System.nanoTime()-LastNanoTime>MinUpdateTime)
            {
                DeltaTime = System.nanoTime() - LastNanoTime;
                LastNanoTime = System.nanoTime();
                if(!GameManager.Paused)
                    Update();
                
                if(!NeedsUpdate || !GridWorld.Children.contains(this))
                    ShouldDestroy = true;
            }
        }
    }
    
    public void Create()
    {
     this.start();   
    }
    
    public void SetPosition(double X,double Y)
    {
        this.X = X;
        this.Y = Y;
        this.TargetX = X;
        this.TargetY = Y;
        this.FromX = X;
        this.FromY = Y;
    }
    
    public void Update()
    {
        InterPol+=MoveSpeed*(double)(DeltaTime)/10000000.0;

       // System.out.println(InterPol);
        TargetX = (Math.floor(TargetX/GridWorld.CellsWidth)+0.5)
                *GridWorld.CellsWidth;
        TargetY = (Math.floor(TargetY/GridWorld.CellsHeight)+0.5)
                *GridWorld.CellsHeight;
        while(InterPol>1)
        {
            InterPol-=1;
            FromX = TargetX;
            FromY = TargetY;
            FindNewTarget();
            X=FromX;
            Y=FromY;
        }
        
        X = FromX + (TargetX - FromX) * InterPol;
        Y = FromY + (TargetY - FromY) * InterPol;
    }
    
    public void Destroy()
    {
        GameManager.MyGrid.DestroyObject(this);
    }
    
    public void FindNewTarget()
    {
        
        
    }
    
    public void Draw(Graphics g)
    {
        
        
    }
    
}
